package com.liyujie.dyeingmachine.ui.home.operate;

import android.os.Bundle;

import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.SPUtils;
import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;
import com.liyujie.dyeingmachine.R;
import com.liyujie.dyeingmachine.databinding.ActivityTemperatureBinding;
import com.liyujie.dyeingmachine.ui.BaseActivity;

/**
 * 温度设置
 */
public class TemperatureActivity extends BaseActivity<ActivityTemperatureBinding> {


    /**
     * VM
     */
    private TemperatureViewModel temperatureViewModel;


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_temperature;

    }


    public void initViewModel() {
        //VM
        temperatureViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(TemperatureViewModel.class);
        binding.setViewModel(temperatureViewModel);

    }


    @Override
    public void initData() {
        super.initData();
        /**
         * 默认烘干温度
         */
        float mDefaultDryingTemperature = SPUtils.getInstance().getFloat("DefaultDryingTemperature", 50);
        binding.DryingTemperatureSeekBar.setProgress(mDefaultDryingTemperature);
        String formatted = String.format("%.1f", mDefaultDryingTemperature);
        binding.DryingTemperatureText.setText("烘干温度" + formatted + "℃");
        /**
         * 默认风速
         */
        float mDefaultWindSpeed = SPUtils.getInstance().getFloat("DefaultWindSpeed", 50);
        binding.WindSpeedSeekBar.setProgress(mDefaultWindSpeed);
        binding.DefaultWindSpeedText.setText("风速 " + ((int) mDefaultWindSpeed) + " %");
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        /**
         *  烘干温度
         */
        binding.DryingTemperatureSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar rangeSeekBar, float leftValue, float rightValue, boolean isFromUser) {

                if (leftValue < 33) {
                    rangeSeekBar.getLeftSeekBar().setThumbDrawableId(R.drawable.thumb_green, rangeSeekBar.getLeftSeekBar().getThumbWidth(), rangeSeekBar.getLeftSeekBar().getThumbHeight());
                } else if (leftValue < 66) {
                    rangeSeekBar.getLeftSeekBar().setThumbDrawableId(R.drawable.thumb_yellow, rangeSeekBar.getLeftSeekBar().getThumbWidth(), rangeSeekBar.getLeftSeekBar().getThumbHeight());
                } else {
                    rangeSeekBar.getLeftSeekBar().setThumbDrawableId(R.drawable.thumb_red, rangeSeekBar.getLeftSeekBar().getThumbWidth(), rangeSeekBar.getLeftSeekBar().getThumbHeight());
                }
                String formatted = String.format("%.1f", leftValue);
                binding.DryingTemperatureText.setText("烘干温度" + formatted + "℃");
                SPUtils.getInstance().put("DefaultDryingTemperature", Float.valueOf(formatted));
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar rangeSeekBar, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar rangeSeekBar, boolean isLeft) {

            }
        });
        /**
         * 默认风速
         */
        binding.WindSpeedSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar rangeSeekBar, float leftValue, float rightValue, boolean isFromUser) {

                if (leftValue < 33) {
                    rangeSeekBar.getLeftSeekBar().setThumbDrawableId(R.drawable.thumb_green, rangeSeekBar.getLeftSeekBar().getThumbWidth(), rangeSeekBar.getLeftSeekBar().getThumbHeight());
                } else if (leftValue < 66) {
                    rangeSeekBar.getLeftSeekBar().setThumbDrawableId(R.drawable.thumb_yellow, rangeSeekBar.getLeftSeekBar().getThumbWidth(), rangeSeekBar.getLeftSeekBar().getThumbHeight());
                } else {
                    rangeSeekBar.getLeftSeekBar().setThumbDrawableId(R.drawable.thumb_red, rangeSeekBar.getLeftSeekBar().getThumbWidth(), rangeSeekBar.getLeftSeekBar().getThumbHeight());
                }
                binding.DefaultWindSpeedText.setText("风速 " + ((int) leftValue) + " %");
                SPUtils.getInstance().put("DefaultWindSpeed", leftValue);
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar rangeSeekBar, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar rangeSeekBar, boolean isLeft) {

            }
        });

    }

}
